package com.pur.zsms.cost.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.pur.zsms.common.exception.GenericException;
import com.pur.zsms.common.support.PageController;
import com.pur.zsms.common.support.PaginationSupport;
import com.pur.zsms.cost.service.ICostService;
import com.pur.zsms.cost.vo.CostVo;
import com.pur.zsms.matermanage.mater.vo.MaterVo;
import com.pur.zsms.ordercustommanage.vo.OrderCustVo;
import com.pur.zsms.ordercustommanage.vo.SelectMaterVo;
import com.pur.zsms.pmsnmanage.role.vo.RoleVo;
import com.pur.zsms.pmsnmanage.user.po.UserPo;
import com.pur.zsms.pmsnmanage.user.po.UserRolePo;
import com.pur.zsms.pmsnmanage.user.service.IUserService;
import com.pur.zsms.pmsnmanage.user.vo.UserVo;
import com.pur.zsms.pmsnmanage.user.web.UserControllor;
import com.pur.zsms.shopmanage.service.IShopService;
import com.pur.zsms.shopmanage.vo.ShopVo;
import com.pur.zsms.supplier.po.ShopSupplierTypePo;
import com.pur.zsms.supplier.po.SupplierPo;
import com.pur.zsms.supplier.service.ISupplierService;
import com.pur.zsms.supplier.vo.ShopSupplierVo;
import com.pur.zsms.supplier.vo.SupplierVo;
import com.pur.zsms.systemsetting.datadictionary.service.IDataDictionaryService;
import com.pur.zsms.turnover.po.TurnoverPo;
import com.pur.zsms.turnover.po.TurnoverSubPo;
import com.pur.zsms.turnover.service.ITurnoverService;
import com.pur.zsms.turnover.vo.TurnoverVo;
import com.pur.zsms.unit.service.IUnitService;
import com.pur.zsms.unit.vo.UnitCommentVo;
import com.pur.zsms.unit.vo.UnitMenuVo;
import com.pur.zsms.unit.vo.UnitVo;

/**
 * 营业额
 * 
 * @author zhou
 * 
 */
@Controller
@RequestMapping(value = "/cost")
public class CostController {
	@SuppressWarnings("unused")
	private static final Logger log = LoggerFactory.getLogger(CostController.class);
	protected int page = 1;

	protected int pageSize = 15;

	PaginationSupport pageList;

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	@Resource(name = "costService")
	private ICostService costService;

	@Resource(name = "dictionaryService")
	private IDataDictionaryService dataDictionaryService;

	@Resource(name = "userService")
	private IUserService userService;

	@Resource(name = "shopService")
	private IShopService shopService;

	@RequestMapping(value = "/listCostSubForUpdate.do")
	@ResponseBody
	public String listCostSubForUpdate(@RequestParam(required = false) String page, @RequestParam(required = false) String rows, CostVo costVo)
			throws GenericException, IllegalAccessException, InvocationTargetException {
		JSONObject json = new JSONObject();
		log.info("----");
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "50";
		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));
		this.pageList = costService.listCostSubList(pageController, costVo);
		List items = pageList.getItems();
		List<CostVo> costVoList = new ArrayList<CostVo>();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				CostVo f = new CostVo();
				f.setCostId(Long.valueOf(item[0].toString()));
				f.setCostSubId(Long.valueOf(item[1].toString()));
				f.setCostType((Integer) item[2]);
				f.setCostValue((String) item[3]);
				costVoList.add(f);
			}
		}
		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", costVoList);
		log.info("----" + json.toString());
		return json.toString();
	}

	@RequestMapping(value = "/listCostSubForLook.do")
	@ResponseBody
	public String listCostSubForLook(@RequestParam(required = false) String page, @RequestParam(required = false) String rows, CostVo costVo)
			throws GenericException, IllegalAccessException, InvocationTargetException {
		JSONObject json = new JSONObject();
		log.info("----");
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "50";
		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));
		this.pageList = costService.listCostSubList(pageController, costVo);
		List items = pageList.getItems();
		List<CostVo> costVoList = new ArrayList<CostVo>();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				CostVo f = new CostVo();
				f.setCostId(Long.valueOf(item[0].toString()));
				f.setCostSubId(Long.valueOf(item[1].toString()));
				f.setCostType((Integer) item[2]);
				f.setCostValue((String) item[3]);
				f.setFileName((String) item[5]);
				costVoList.add(f);
			}
		}
		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", costVoList);
		log.info("----" + json.toString());
		return json.toString();
	}

	@RequestMapping(value = "/costTypePullDown.do", method = { RequestMethod.POST })
	public void costTypePullDown(MaterVo materVo, HttpServletRequest request, HttpServletResponse response) {
		JSONObject json = new JSONObject();
		json.put("success", true);
		response.setContentType("text/json;charset=UTF-8");
		PrintWriter writer;
		try {
			String materTypeString = dataDictionaryService.pullDownList("costType", "costType", "costTypeName");
			writer = response.getWriter();
			writer.write(materTypeString);
			writer.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (GenericException e) {
			e.printStackTrace();
		}
		// return json.toString();
	}

	@RequestMapping(value = "/costTypeFormate.do", method = { RequestMethod.POST })
	public void costTypeFormate(CostVo costVo, HttpServletRequest request, HttpServletResponse response) {
		JSONObject json = new JSONObject();
		json.put("success", true);
		response.setContentType("text/json;charset=UTF-8");
		PrintWriter writer;
		try {
			String costTypeString = dataDictionaryService.getCostType();
			writer = response.getWriter();
			writer.write(costTypeString);
			writer.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (GenericException e) {
			e.printStackTrace();
		}
		// return json.toString();
	}

	@RequestMapping(value = "/costDetailForManager.do")
	@ResponseBody
	public String costDetailForManager(@RequestParam(required = false) String page, @RequestParam(required = false) String rows, CostVo costVo)
			throws GenericException, IllegalAccessException, InvocationTargetException {
		JSONObject json = new JSONObject();
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "50";
		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));
		this.pageList = costService.costDetailForManager(pageController, costVo);
		List items = pageList.getItems();
		List<CostVo> costVoList = new ArrayList<CostVo>();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				CostVo f = new CostVo();
				f.setCostType((Integer) item[0]);
				f.setCostValueAll(item[1].toString());
				costVoList.add(f);
			}
		}
		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", costVoList);
		return json.toString();
	}
}
